package com.boding.power.controller;

import java.util.Date;

import org.apache.catalina.User;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;

import com.boding.power.common.Constants;
import com.boding.power.model.SysDict;
import com.boding.power.model.SysLog;
import com.boding.power.utils.DataGrid;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

/**
 * 字典项管理
 * 
 * @author giegie
 * @date 2016年5月13日
 * @description
 * 
 */
public class SysController extends Controller {
	public void listDictUI() {
		render("dictList.jsp");
	}

	public void listDict() {
		String searchField = getPara("searchField");
		String searchString = getPara("searchString");
		Page<SysDict> page = SysDict.dao.findByPage(1, 10, searchField,
				searchString);
		DataGrid<SysDict> dataGrid = new DataGrid<SysDict>(page);
		renderJson(dataGrid);
	}

	public void operateDict() {
		// jqgrid 传递的不是 dict.id而是 id 所以把 modelname 设置为""
		SysDict dict = getModel(SysDict.class, "", true);
		String oper = getPara("oper");
		if ("add".equals(oper)) {
			dict.setCreateBy(SecurityUtils.getSubject().getPrincipal()
					.toString());
			dict.setCreateDate(new Date());
			dict.setDelFlag(Constants.FLAG_NORMAL);
			dict.save();
		} else if ("edit".equals(oper)) {
			dict.update();
		} else if ("del".equals(oper)) {
			dict.setDelFlag(Constants.FLAG_DEL);
			dict.update();
		}
		renderText("success");
	}

	public void listSysLogUI() {
		render("sysLogList.jsp");
	}

	public void listSysLog() {
		String searchField = getPara("searchField");
		String searchString = getPara("searchString");
		Page<SysLog> page = SysLog.dao.findByPage(1, 10, searchField,
				searchString);
		DataGrid<SysLog> dataGrid = new DataGrid<SysLog>(page);
		renderJson(dataGrid);
	}
	
	public void operateSysLog() {
		SysLog log = getModel(SysLog.class, "", true);
		String oper = getPara("oper");
		 if ("del".equals(oper)) {
			 log.deleteById(log);
		}
		renderJson("success");
	}
}
